<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var dog=function(){
            var _age;  //私有成员年龄
            var obj={};  //要返回的对象
            Object.defineProperty(obj,"age",{  //定义age属性
                get:function(){
                    return _age;
                },
                set:function(value){
                    if(!isNaN(value)&&value>=0&&value<=100){
                        _age=value;
                    }
                    else{
                        throw "年龄必须0-100的数字";
                    }
                }
            });
            return obj;
        }();

        dog.age=5;
        console.log(dog.age);
        console.log(dog._age);
        dog.age="five";
    </script>
</body>
</html>